package com.edu.internship.busi.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.internship.busi.dto.RoleDTO;
import com.edu.internship.busi.entity.Permission;
import com.edu.internship.busi.entity.Role;
import com.edu.internship.busi.entity.RolePermission;
import com.edu.internship.busi.service.IPermissionService;
import com.edu.internship.busi.service.IRolePermissionService;
import com.edu.internship.busi.service.IRoleService;
import com.edu.internship.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("api/role")
@Slf4j
@Api(tags = {"角色的控制器"})
public class RoleController {

    @Resource
    private IRoleService roleService;
    @Resource
    private IPermissionService permissionService;
    @Resource
    private IRolePermissionService rolePermissionService;

    @GetMapping
    @ApiOperation(value = "分页查询-Role", notes = "根据条件进行查询-Role")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", dataTypeClass = Long.class, paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页几条记录", dataTypeClass = Long.class, paramType = "query", defaultValue = "10")
    })
    public IPage<Role> queryForRole(RoleDTO dto, @RequestParam(defaultValue = "1") long current,
                                    @RequestParam(defaultValue = "10") long size){
        LambdaQueryWrapper<Role> reQw = Wrappers.lambdaQuery();
        reQw.like(StringUtils.isNotBlank(dto.getRoleName()),Role::getRoleName,dto.getRoleName());

        return roleService.page(new Page<>(current,size),reQw);
    }


    @PostMapping("/created")
    @ApiOperation(value = "新增-Role", notes = "新增-Role")
    public String created(@RequestBody @Validated RoleDTO dto){
        dto.setCreatedTime(LocalDateTime.now());
        dto.setId(Tools.createRandomCode(5,true));
        roleService.created(dto);
        return dto.getId();
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除-Role",notes = "删除-Role")
    public boolean remove(@RequestBody @Validated RoleDTO dto){
//        LambdaQueryWrapper<RolePermission> rpQw = Wrappers.lambdaQuery();
//        rpQw.eq(StringUtils.isNotBlank(dto.getId()),RolePermission::getRId,dto.getId());
//        rolePermissionService.remove(rpQw);
        return roleService.remove(dto.getId());
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新-Role",notes = "更新-Role")
    public boolean update(@RequestBody @Validated RoleDTO dto){
        return roleService.update(dto);
    }

    @GetMapping("queryById")
    @ApiOperation(value = "根据id查询-Role",notes ="根据id查询-Role")
    public Role queryById(String id){
        return roleService.getById(id);
    }

    @PostMapping("/change")
    @ApiOperation(value = "禁用启用",notes = "禁用启用")
    public boolean change(@RequestBody List<String> id){
        List<Role> role = roleService.change(id);
        if (role.get(0).getStatus().equals("1")) {
            role.get(0).setStatus("2");
        } else {
            role.get(0).setStatus("1");
        }
        roleService.updateById(role.get(0));

        return true;
    }

    @GetMapping("/get")
    @ApiOperation(value = "查询Permission",notes = "查询Permission")
    public List<String> getRole(){
        LambdaQueryWrapper<Role> pQw = Wrappers.lambdaQuery();
        pQw.eq(Role::getStatus,1);
        return roleService.list(pQw).stream().map(Role::getRoleDesc).collect(Collectors.toList());
    }




}
